/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.com.pps.web.common.component.table;

import java.util.ArrayList;
import java.util.Iterator;

public class CacheModel extends AbstractModel {

    protected ArrayList m_cachedData;
    private String m_sortColumn = "";
    private boolean m_sortAscending;
    private boolean m_newSortColumn;
    private boolean m_newSortAscending;

    public CacheModel() {
        this.m_cachedData = new ArrayList();
    }

    public CacheModel(Class pm_itemClass) {
        super(pm_itemClass);
        this.m_cachedData = new ArrayList();
    }

    public void setAllSelectedStatus(boolean pm_status) {
        Iterator iter = this.m_cachedData.iterator();
        while (iter.hasNext()) {
            AbstractTableItem item = (AbstractTableItem) iter.next();
            item.setSelected(pm_status);
        }
    }

    public String getSort() {
        return this.m_sortColumn;
    }

    public void setSort(String pm_column) {
        if ((this.m_sortColumn == null) || (!this.m_sortColumn.equals(pm_column))) {
            this.m_newSortColumn = true;
        } else {
            this.m_newSortColumn = false;
        }
        this.m_sortColumn = pm_column;
    }

    public boolean isSortAscending() {
        return this.m_sortAscending;
    }

    public void setSortAscending(boolean pm_sortAscending) {
        if (this.m_sortAscending != pm_sortAscending) {
            this.m_newSortAscending = true;
        } else {
            this.m_newSortAscending = false;
        }
        if ((this.m_newSortAscending) && (!this.m_newSortColumn)) {
            this.m_sortAscending = pm_sortAscending;
        }

        if ((!this.m_newSortColumn) && (!this.m_newSortAscending)) {
            return;
        }
    }

    public void setTargetPage(int pm_value) {
        this.m_targetPage = pm_value;
    }

    public int getTargetPage() {
        return this.m_targetPage;
    }

    @Override
    public synchronized boolean getHasNextPage() {
        ;
        return this.m_selectedPage != this.m_pageCount;
    }

    @Override
    public synchronized boolean getHasPreviousPage() {
        return this.m_selectedPage > 1;
    }

    @Override
    public synchronized void firstPage() {
        this.m_selectedPage = (this.m_targetPage = 0);
        nextPage();
    }

    @Override
    public synchronized void lastPage() {
        this.m_selectedPage = (this.m_targetPage = this.m_pageCount);
        loadPage();
    }

    @Override
    public synchronized void nextPage() {
        this.m_selectedPage = (this.m_targetPage = Math.min(this.m_pageCount, this.m_selectedPage + 1));
        loadPage();
    }

    @Override
    public synchronized void previousPage() {
        this.m_selectedPage = (this.m_targetPage = Math.max(0, this.m_selectedPage - 1));
        if ((this.m_selectedPage == 0) && (this.m_pageCount != 0)) {
            this.m_selectedPage = (this.m_targetPage = 1);
        }
        loadPage();
    }

    protected void loadPage() {
        newPage();
        if ((this.m_selectedPage == 0) || (this.m_selectedPage > this.m_pageCount)) {
            return;
        }
        int iCursor = (this.m_selectedPage - 1) * this.m_maximumPageSize;
        int iCount = 0;
        while ((iCount < this.m_maximumPageSize) && (iCursor < this.m_cachedData.size())) {
            addItem((AbstractTableItem) this.m_cachedData.get(iCursor));

            ++iCount;
            ++iCursor;
        }
    }

    public synchronized void displayTargetPage() {
        this.m_selectedPage = this.m_targetPage;
        loadPage();
    }
}
